package utils.queue;

/**
 * @author yaohuaiying
 * @create 2018-02-09 17:38
 **/
public class MyQueue {
    private QueueNode head;
    private QueueNode end;
    private int length = 0;

    public MyQueue() {
    }

    public MyQueue(int data) {
        this.head = new QueueNode(data);
        this.end = head;
        length = 1;
    }

    /**
     * 入队的时候 在尾部插入
     * @param data
     */
    public void qAdd(int data){
        if(end == null){
            this.head = new QueueNode(data);
            this.end = head;
            length = 1;
        }else {
            this.end.setNext(new QueueNode(data));
            this.end = this.end.getNext();
            length++;
        }
    }

    /**
     * 出队的时候 在头部出队
     * @return
     */
    public int qDel(){
        if(length == 0){
            return -1;
        }else if(length == 1){
            int result = this.head.getData();
            this.head = null;
            this.end = null;
            length--;
            return result;
        }else {
            int result = this.head.getData();
            this.head = this.head.getNext();
            length--;
            return result;
        }
    }

    /**
     * 从尾到头打印 最后一个先出队
     */
    public static void print(QueueNode currentNode){
        if(currentNode == null){
            return;
        }else {
            print(currentNode.getNext());
            System.out.print(currentNode.getData() + " ");
        }
    }

    public QueueNode getHead() {
        return head;
    }

    public void setHead(QueueNode head) {
        this.head = head;
    }

    public QueueNode getEnd() {
        return end;
    }

    public void setEnd(QueueNode end) {
        this.end = end;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
